package com.utils;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 * @author :wn
 * @program : 留言板
 * @descript :
 * @create :2020-12-07 10:42
 */
@WebServlet("/getSecurityCode")
public class SecurityCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        Map map = SecurityCode.generateCodeAndPic();
        ServletOutputStream out = response.getOutputStream();
        BufferedImage img = (BufferedImage) map.get("codePic");
        String securityCode = (String) map.get("code");
        HttpSession session=req.getSession();
        session.setAttribute("securityCode", securityCode);
        ImageIO.write(img, "jpg", response.getOutputStream());
    }
}
